package com.hjj.spring.security.oauth2.server.configure;

import com.hjj.spring.security.oauth2.server.domain.TbPermission;
import com.hjj.spring.security.oauth2.server.domain.TbUser;
import com.hjj.spring.security.oauth2.server.service.TbPermissionService;
import com.hjj.spring.security.oauth2.server.service.TbUserService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private TbUserService tbUserService;
    @Autowired
    private TbPermissionService tbPermissionService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        //数据库查询
        TbUser tbUser = tbUserService.getByUsername(s);

        List<GrantedAuthority> grantedAuthorities = Lists.newArrayList();

        //tbUser , 则授权
        if (tbUser != null) {
            //查询user权限
            List<TbPermission> tbPermissions = tbPermissionService.selectByUserId(tbUser.getId());

            tbPermissions.forEach(tbPermission -> {
                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(tbPermission.getEnname());
                grantedAuthorities.add(grantedAuthority);
            });

            return new User(tbUser.getUsername() , tbUser.getPassword() , grantedAuthorities); //用户名 , 密码 , 授权
        }

        return null;
    }
}
